<?php
namespace app\index\controller;
use app\model\Address as AddressModel;

use think\Db;


class Address extends Base
{   
    // 列表
    public function index(){
        $user_uuid = $this->login_user['uuid'];
        $map_params = [
      
        ];
        $map = getSearchParam($map_params);
        $map[] = ['user_uuid','=',$user_uuid];
      
        $model = new AddressModel();
        $page_param = ['page'=>input('page_index',1),'list_rows'=>input('page_size',10)];
        $list = $model->where($map)->order('create_time desc')->paginate($page_param)->toarray();
        foreach ($list['data'] as $k => $vo) {
            
        }

        return apiResult('2000','获取成功',$list);
    }

    // 详情
    public function read($uuid=''){
        $user_uuid = $this->login_user['uuid'];
        $map = [
            ['user_uuid','=',$user_uuid]
        ];
        if (!empty($uuid)) {
            $map[] = ['uuid','=',$uuid];
        }else{
            $map[] = ['is_default','=',1];
        }

        $model = new AddressModel();
        $data = $model->where($map)->find();

        return apiResult('2000','获取成功',$data);
    }

    // 新增
    public function save(){
        $user_uuid = $this->login_user['uuid'];
        $param = file_get_contents('php://input');
        $param = json_decode($param,true);

        $fields = [
            'must'=>['truename','mobile','address'],
            'nomust'=>['status','is_default']
        ];
        $save_data = paramFilter($param,$fields);
        if (!empty($save_data['error_msg'])) {
            return apiResult('5000',$save_data['error_msg']);
        }

        $model = new AddressModel();
        // 启动事务 
        Db::startTrans();
        try{
            if ($save_data['is_default'] == 1) {
                $model->where([
                    ['user_uuid','=',$user_uuid],
                    ['is_default','=',1]
                ])->update(['is_default'=>0]);
            }elseif($model->where([
                    ['user_uuid','=',$user_uuid],
                    ['is_default','=',1]
                ])->count() == 0){
                $save_data['is_default'] = 1;             
            }

            $save_data['uuid'] = uuidCreate();
            $save_data['create_time'] = date('Y-m-d H:i:s');
            $save_data['user_uuid'] = $user_uuid;
            if ( !$model->save($save_data) ) {
                throw new \Exception("保存失败");
            }

            // 更新成功 提交事务
            Db::commit();

            return apiResult('2000','提交成功',$save_data['uuid']);
        } catch (\Exception $e) {
            // 更新失败 回滚事务
            Db::rollback();
            return apiResult('5000',$e->getMessage());
        }
    }

    // 更新
    public function update(){
        $user_uuid = $this->login_user['uuid'];
        $param = file_get_contents('php://input');
        $param = json_decode($param,true);

        if ( empty($param['uuid']) ) {
            exception('uuid不能为空',400);
        }

        $fields = [
            'nomust'=>['truename','mobile','address','status','is_default']
        ];
        $save_data = paramFilter($param,$fields);
        if (!empty($save_data['error_msg'])) {
            return apiResult('5000',$save_data['error_msg']);
        }
        if (empty($save_data)) {
            return apiResult('5000','无任何修改');
        }
  
        $model = new AddressModel();
        // 启动事务 
        Db::startTrans();
        try{
            if ($save_data['is_default'] == 1) {
                $model->where([
                    ['user_uuid','=',$user_uuid],
                    ['is_default','=',1]
                ])->update(['is_default'=>0]);
            }

            if ( $model->where('uuid',$param['uuid'])->update($save_data) === false ) {
                throw new \Exception("保存失败");
            }

            // 更新成功 提交事务
            Db::commit();

            return apiResult('2000','更新成功');
        } catch (\Exception $e) {
            // 更新失败 回滚事务
            Db::rollback();
            return apiResult('5000',$e->getMessage());
        }
    }

    // 删除
    public function delete($uuid){
        $model = new AddressModel();
        Db::startTrans();
        try{
            if ( !$model->where('uuid',$uuid)->delete() ) {
                throw new \Exception("删除失败");
            }

            // 更新成功 提交事务
            Db::commit();
            return apiResult('2000','删除成功');
        } catch (\Exception $e) {
            // 更新失败 回滚事务
            Db::rollback();
            return apiResult('5000',$e->getMessage());
        }
    }
   


}
